//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CipherInputStream extends FilterInputStream {
    private javax.vpncrypto.Cipher cipher;
    private InputStream input;
    private byte[] ibuffer = new byte[512];
    private boolean done = false;
    private byte[] obuffer;
    private int ostart = 0;
    private int ofinish = 0;
    private boolean closed = false;

    private int getMoreData() throws IOException {
        if (this.done) {
            return -1;
        } else {
            int var1 = this.input.read(this.ibuffer);
            if (var1 == -1) {
                this.done = true;

                try {
                    this.obuffer = this.cipher.doFinal();
                } catch (javax.vpncrypto.BadPaddingException | javax.vpncrypto.IllegalBlockSizeException var3) {
                    this.obuffer = null;
                    throw new IOException(var3);
                }

                if (this.obuffer == null) {
                    return -1;
                } else {
                    this.ostart = 0;
                    this.ofinish = this.obuffer.length;
                    return this.ofinish;
                }
            } else {
                try {
                    this.obuffer = this.cipher.update(this.ibuffer, 0, var1);
                } catch (IllegalStateException var4) {
                    this.obuffer = null;
                    throw var4;
                }

                this.ostart = 0;
                if (this.obuffer == null) {
                    this.ofinish = 0;
                } else {
                    this.ofinish = this.obuffer.length;
                }

                return this.ofinish;
            }
        }
    }

    public CipherInputStream(InputStream var1, Cipher var2) {
        super(var1);
        this.input = var1;
        this.cipher = var2;
    }

    protected CipherInputStream(InputStream var1) {
        super(var1);
        this.input = var1;
        this.cipher = new NullCipher();
    }

    public int read() throws IOException {
        if (this.ostart >= this.ofinish) {
            int var1;
            for(var1 = 0; var1 == 0; var1 = this.getMoreData()) {
            }

            if (var1 == -1) {
                return -1;
            }
        }

        return this.obuffer[this.ostart++] & 255;
    }

    public int read(byte[] var1) throws IOException {
        return this.read(var1, 0, var1.length);
    }

    public int read(byte[] var1, int var2, int var3) throws IOException {
        int var4;
        if (this.ostart >= this.ofinish) {
            for(var4 = 0; var4 == 0; var4 = this.getMoreData()) {
            }

            if (var4 == -1) {
                return -1;
            }
        }

        if (var3 <= 0) {
            return 0;
        } else {
            var4 = this.ofinish - this.ostart;
            if (var3 < var4) {
                var4 = var3;
            }

            if (var1 != null) {
                System.arraycopy(this.obuffer, this.ostart, var1, var2, var4);
            }

            this.ostart += var4;
            return var4;
        }
    }

    public long skip(long var1) throws IOException {
        int var3 = this.ofinish - this.ostart;
        if (var1 > (long)var3) {
            var1 = (long)var3;
        }

        if (var1 < 0L) {
            return 0L;
        } else {
            this.ostart = (int)((long)this.ostart + var1);
            return var1;
        }
    }

    public int available() throws IOException {
        return this.ofinish - this.ostart;
    }

    public void close() throws IOException {
        if (!this.closed) {
            this.closed = true;
            this.input.close();
            if (!this.done) {
                try {
                    this.cipher.doFinal();
                } catch (IllegalBlockSizeException | BadPaddingException var2) {
                }
            }

            this.ostart = 0;
            this.ofinish = 0;
        }
    }

    public boolean markSupported() {
        return false;
    }
}
